/**
 * May 2, 2014 10:40:43 AM
 */
package elitech.vietnam.copyweatherfashion.fragments.today;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.copyweatherfashion.LoginActivity;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment.ConfirmDialogClick;
import elitech.vietnam.copyweatherfashion.dialog.ConfirmDialogFragment.Type;
import elitech.vietnam.copyweatherfashion.dialog.WarningDialogFragment;
import elitech.vietnam.copyweatherfashion.entities.Post;
import elitech.vietnam.copyweatherfashion.fragments.StorePostFragment;
import elitech.vietnam.copyweatherfashion.widget.ButtonLike;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

/**
 * @author Cong
 *
 */
public class TodayPagerFragment extends Fragment implements View.OnClickListener, ConfirmDialogClick {
	
	private static final String DATA_KEY = "DATA_KEY";
	
	MainActivity mActivity;
	
	Post mPost;
	ImageView mImage, mMask;
	public ButtonLike mBtnLike;
	
	public static TodayPagerFragment newInstance(int pos) {
		TodayPagerFragment fragment = new TodayPagerFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(DATA_KEY, pos);
		fragment.setArguments(bundle);
		return fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.today_item, container, false);
		
		mImage = (ImageView) view.findViewById(R.id.todayitem_image);
		mMask = (ImageView) view.findViewById(R.id.todayitem_mask);
		mBtnLike = (ButtonLike) view.findViewById(R.id.btnLike);
		
		mMask.setOnClickListener(this);
		mBtnLike.setOnClickListener(this);
		
		return view;
	}
	
	@Override
	public void onStart() {
		Log.w(getClass().getSimpleName(), "onStart " + getArguments().getInt(DATA_KEY));
		super.onStart();
		mPost = mActivity.mTodayList.get(getArguments().getInt(DATA_KEY));
		loadData(mPost);
	}
	
	public void loadData(Post post) {
		UrlImageViewHelper.setUrlDrawable(mImage, Const.SERVER_IMAGE_URL + post.Image);
		mBtnLike.setData(post.Liked(), post.Likes);
	}
	
	@Override
	public void onResume() {
		Log.w(getClass().getSimpleName(), "onResume");
		super.onResume();
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mPost = null;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.todayitem_mask:
			mActivity.mShopPost = mPost;
			Log.w("mActivity.mShopPost", mActivity.mShopPost.toString());
			mActivity.getCurrentBase().replaceFragment(new StorePostFragment(), true);
			break;
		case R.id.btnLike:
			if (mActivity.mUser == null) {
				Intent intent = new Intent(mActivity, LoginActivity.class);
				mActivity.startActivityForResult(intent, MainActivity.REQUEST_LOGIN);
			} else {
				final String ms = (mPost.Liked())
					? getString(R.string.do_you_want_to_unlike_this_post_)
					: getString(R.string.do_you_want_to_like_this_post_);
				new ConfirmDialogFragment.Builder()
					.setMessage(ms)
					.setTargetFragment(this)
					.setType(Type.FRAGMENT)
					.build().show(getFragmentManager());
			}
			break;
		default:
			break;
		}
	}
	
	class LikeTask extends AsyncTask<Integer, Integer, Integer> {
		@Override
		protected void onPreExecute() {
			mBtnLike.setEnabled(false);
		}
		@Override
		protected Integer doInBackground(Integer... params) {
			try {
				return new Service().newLike(mActivity.mUser.Id, mPost.Id, mPost.Liked() ? -1 : 1);
			} catch (IOException | XmlPullParserException e) { e.printStackTrace();
				return null;
			}
		}
		@Override
		protected void onPostExecute(Integer result) {
			if (!isVisible())
				return;
			mBtnLike.setEnabled(true);
			if (result == null || result == -1) {
				new WarningDialogFragment.Builder()
				.setMessage(getString(R.string.cannot_connect_to_server))
				.build().show(getFragmentManager());
			} else {
				mPost.Liked(!mPost.Liked());
				mPost.Likes = result;
				mBtnLike.setData(mPost.Liked(), mPost.Likes);
			}
		}
	}

	@Override
	public void yesClick() {
		new LikeTask().execute();
	}
	@Override
	public void noClick() {
	}
}
